﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PIF1006_TP2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypter_Click(object sender, EventArgs e)
        {
            string result = Chiffrement.Chiffrer(rTxtClear.Text, txtKey.Text);
            rTxtEncrypted.Text = result;

            rTxtClear.Clear();
            btnDecrypter.Enabled = true;
            btnEncrypter.Enabled = false;
        }

        private void btnDecrypter_Click(object sender, EventArgs e)
        {
            string result = String.Empty;

            try
            {
                result = Chiffrement.Dechiffrer(rTxtEncrypted.Text, txtKey.Text);
            }
            catch(IndexOutOfRangeException)
            {
                MessageBox.Show("clé invalide.");
            }

            if (!String.IsNullOrEmpty(result))
            {
                rTxtClear.Text = result;

                rTxtEncrypted.Clear();
                btnDecrypter.Enabled = false;
                btnEncrypter.Enabled = true;
            }
        }

        private void txtKey_TextChanged(object sender, EventArgs e)
        {
            btnEncrypter.Enabled = !String.IsNullOrEmpty(txtKey.Text) && !String.IsNullOrEmpty(rTxtClear.Text);
            btnDecrypter.Enabled = !String.IsNullOrEmpty(txtKey.Text) && !String.IsNullOrEmpty(rTxtEncrypted.Text);
        }
    }
}
